package com.derbysoft.lowcostadapter.dswitch2.translator.impl;

import com.derbysoft.remote.dswitch.core.*;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRQ;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRS;
import com.derbysoft.remote.dswitch.hotel.dto.HotelAvailRoomStayDTO;
import org.apache.commons.lang.StringUtils;

/**
 * User: jason
 * Date: 2012-11-01
 */
public abstract class TranslateHelper {

    private static final String NO_RESULT = "There are no results for your Search";

    public static GenericRS<HotelAvailRS> buildFailedResponse(String errorMessage, GenericRQ<HotelAvailRQ> rq) {
        GenericRS<HotelAvailRS> genericRS = new GenericRS<HotelAvailRS>();
        genericRS.setHeader(createHeader(rq));
        if (StringUtils.contains(errorMessage, NO_RESULT)) {
            genericRS.setSuccess(new SuccessDTO());
            HotelAvailRS hotelAvailRS = new HotelAvailRS();
            HotelAvailRoomStayDTO hotelAvailRoomStay = new HotelAvailRoomStayDTO();
            hotelAvailRoomStay.setHotelRef(rq.getPayload().getAvailCriteria().getHotelRef());
            hotelAvailRS.setHotelAvailRoomStay(hotelAvailRoomStay);
            genericRS.setPayload(hotelAvailRS);
            return genericRS;
        }
        ErrorsDTO errors = new ErrorsDTO();
        ErrorDTO errorDTO = new ErrorDTO();
        errorDTO.setCode("System");
        errorDTO.setSource("lowcostbeds provider side");
        errorDTO.setMessage(errorMessage);
        errors.getErrorList().add(errorDTO);
        genericRS.setErrors(errors);
        return genericRS;
    }

    protected static ResponseHeader createHeader(GenericRQ<HotelAvailRQ> rq) {
        ResponseHeader responseHeader = new ResponseHeader();
        responseHeader.setTaskId(rq.getHeader().getTaskId());
        return responseHeader;
    }
}
